package com.yenlien.traceyun.control.service;


import com.yenlien.traceyun.control.config.Log;
import com.yenlien.traceyun.control.dao.WordDao;
import com.yenlien.traceyun.control.entity.Word;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class WordService {
    @Autowired
    private WordDao wordDao;

    /**
     * 新增意见
     *
     * @param word
     */
    @Transactional
    @Log(operation = "意见反馈", profession = "新增意见")
    public void save(Word word) {
        word.setCreatetime(new Date());
        if (!StringUtils.isEmpty(word)) {
            wordDao.save(word);
        }
    }

    /**
     * 查询意见
     *
     * @param page
     * @return
     */
    public Page<Word> select(Integer page) {
        Sort ageSort = Sort.by(Sort.Direction.DESC, "createtime");
        PageRequest pageRequest = PageRequest.of(page, 10, ageSort);
        Page<Word> all = wordDao.findAll(pageRequest);
        return all;
    }

    /**
     * 根据标题模糊搜索
     *
     * @param captions
     * @return
     */
    public Object search(String captions) {
        return wordDao.findByCaptionLike(captions);
    }

    /**
     * 根据id查询意见
     *
     * @param id
     * @return
     */
    public Object selectId(String id) {
        return wordDao.findById(id);
    }

}
